package it.diego.projecttwo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class DragAndDropActivity extends Activity implements OnClickListener{

	private View selected_item = null;
    private int offset_x = 0;
    private int offset_y = 0;
    int status = 0;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drag_and_drop);
        
       
    	 ImageView img = (ImageView)findViewById(R.id.img);
         img.setOnTouchListener(new View.OnTouchListener() {
                        
                         @Override
                         public boolean onTouch(View v, MotionEvent event) {
                                 switch(event.getActionMasked())
                                 {
                                         case MotionEvent.ACTION_DOWN:
                                                 offset_x = (int)event.getX();
                                                 offset_y = (int)event.getY();
                                                 selected_item = v;
                                                 break;
                                         default:
                                                 break;
                                 }
                                 status = 1;
                                 return false;
                         }
                 });
    	
    	 ViewGroup vg = (ViewGroup)findViewById(R.id.vg);
         vg.setOnTouchListener(new View.OnTouchListener() {
                        
                         @Override
                         public boolean onTouch(View v, MotionEvent event) {
                                 switch(event.getActionMasked())
                                 {
                                         case MotionEvent.ACTION_MOVE:
                                        	 if(status==1)
                                        	 {
                                                 int x = (int)event.getX() - offset_x;
                                                 int y = (int)event.getY() - offset_y;

						                         int w = getWindowManager().getDefaultDisplay().getWidth() - 100;
						                         int h = getWindowManager().getDefaultDisplay().getHeight() - 100;
						                         if(x > w)
						                             x = w;
						                         if(y > h)
						                             y = h;
		                                          LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
		                                         new ViewGroup.MarginLayoutParams(
		                                                         LinearLayout.LayoutParams.WRAP_CONTENT,
		                                                         LinearLayout.LayoutParams.WRAP_CONTENT));
		                                          lp.setMargins(x, y, 0, 0);

                                                 selected_item.setLayoutParams(lp);
                                                 
                                        	 }
                                                 break;
                                         case MotionEvent.ACTION_UP:
                                        	 status=0;
                                             break;
                                         default:
                                             break;
                                 }
                                 return true;
                         }
         });
       
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.drag_and_drop, menu);
        return true;
    }
    
    @Override
    public void onClick(View v) {
    	switch (v.getId()) 
		{
			case R.id.cambiaTutorial:
			{
				startActivity(new Intent(this, ImageGalleryActivity.class));
				break;
			}
		}
    }
}
